package com.bbs.controller;

import cn.hutool.db.PageResult;
import com.bbs.dto.MyPageResult;
import com.bbs.dto.PostDTO;
import com.bbs.dto.PostPageQueryDTO;
import com.bbs.dto.Result;
import com.bbs.entity.Post;
import com.bbs.service.PostService;
import com.bbs.service.impl.PostServiceImpl;
import com.bbs.vo.PostVO;
import com.github.pagehelper.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/posts")
@Tag(name = "帖子管理", description = "帖子相关的接口")
public class PostController {
    
    @Autowired
    private PostService postService;
    @Autowired
    private PostServiceImpl postServiceImpl;

    @PostMapping
    @Operation(summary = "创建帖子", description = "创建新帖子")
    public Result createPost(@RequestBody PostDTO postDTO) {
        PostVO postVO = postService.createPost(postDTO);
        return Result.ok(postVO);
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "获取帖子信息", description = "根据帖子ID获取帖子信息")
    @Parameter(name = "id", description = "帖子ID", required = true)
    public Result getPostById(@PathVariable Long id) {
        PostVO postVO = postService.getPostById(id);
        return Result.ok(postVO);
    }
    
    @GetMapping
    @Operation(summary = "获取所有帖子", description = "获取系统中所有帖子")
    public Result getAllPosts() {
        return Result.ok(postService.getAllPosts());
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新帖子", description = "根据帖子ID更新帖子信息")
    @Parameter(name = "id", description = "帖子ID", required = true)
    public Result updatePost(@PathVariable Long id, @RequestBody PostDTO postDTO) {
        PostVO postVO = postService.updatePost(id, postDTO);
        return Result.ok(postVO);
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除帖子", description = "根据帖子ID删除帖子")
    @Parameter(name = "id", description = "帖子ID", required = true)
    public Result deletePost(@PathVariable Long id) {
        postService.deletePost(id);
        return Result.ok();
    }
    
    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户帖子", description = "获取指定用户的所有帖子")
    @Parameter(name = "userId", description = "用户ID", required = true)
    public Result getPostsByUserId(@PathVariable Long userId) {
        return Result.ok(postService.getPostsByUserId(userId));
    }
    
    @PutMapping("/{id}/view")
    @Operation(summary = "增加浏览量", description = "增加帖子的浏览量")
    @Parameter(name = "id", description = "帖子ID", required = true)
    public Result incrementViewCount(@PathVariable Long id) {
        postService.incrementViewCount(id);
        return Result.ok();
    }

    @GetMapping("/top5/{id}")
    @Operation(summary ="获取帖子前五个点赞的用户",description = "获取前五个点赞用户")
    public Result getTOP5LikeByPostId(@PathVariable Long id){
       return  postService.getTOP5LikeByPostId(id);
    }
@GetMapping("/of/follow")
    @Operation(summary = "在关注页面获取关注的人的博客",description = "滚动分页查询")
//RequestParam接收地址栏传参//offset代表跳过几个,max本次查询最大值(Zset权重)
    public Result queryBlogFollow(@RequestParam("lastId")Long max,@RequestParam(value = "offset",defaultValue ="0")Integer offset){
return Result.ok();
        //PostService.queryPostOfFollow(max,offset);
    }
    //分页查询接口
    @PostMapping("page/")
public Result pageResult(@RequestBody PostPageQueryDTO postPageQueryDTO){
        MyPageResult<PostVO> result=postService.pageQuery(postPageQueryDTO);
       return Result.ok(result);
    }

    //添加帖子浏览数
    @GetMapping("/viewCount")
    @Operation(summary = "修改帖子浏览数",description = "仅为测试lambdaUpdate")
    public Result updateViewCount(@RequestParam("id")Long id,@RequestParam("num")Integer num){
return postService.updateViewCount(id,num);
    }



}